<?php
/**
 * Created by PhpStorm.
 * User: ljc
 * Date: 2018/7/23
 * Time: 9:26
 */

namespace Modules\Shop\Service;
use Blade;
use Modules\Shop\Entities\Article;
use Modules\Shop\Entities\Category;
use Modules\Shop\Entities\Tag;
use Modules\Shop\Entities\TagArticle;
use Illuminate\Pagination\LengthAwarePaginator;
class TagService
{
	/**
	 * 创建标签的方法，在这里面调用各个自定义标签方法
	 */
	public function make(){
		$this->category ();
		$this->article();
		$this->tag();
		$this->currentCategory();
		$this->pre_next();
		$this->friend ();
	}
	/**
	 * 自定义标签，用来获取文章
	 */
	public function article(){
		Blade::directive ('article',function($canshu){
			$canshu=$canshu?:'[]';
			$php=<<<php
<?php 
	\$params=$canshu;
	\$db=new Modules\Shop\Entities\Article();
	if(isset(\$params['pid'])){
		\$db=\$db->where('pid',\$params['pid']);
	}
	if(isset(\$params['iscommend'])){
		\$db=\$db->where('iscommend',\$params['iscommend']);
	}
	if(isset(\$params['hot'])){
		\$db=\$db->orderBy('click','desc');
	}
	if(isset(\$params['orderBy_time'])){
		\$db=\$db->orderBy('created_at','desc');
	}
	if(isset(\$params['limit'])){
		\$db=\$db->limit(\$params['limit']);
	}
	if(isset(\$params['pre'])){
		\$db=\$db->where('id','<',\$params['pre'])->limit(1);
	}
	if(isset(\$params['next'])){
		\$db=\$db->where('id','>',\$params['next'])->limit(1);
	}
	if(isset(\$params['page'])){
		\$data=\$db->paginate(\$params['page']);
	}else{
		\$data=\$db->get();
	}
	foreach(\$data as \$article){
	\$article['url']='/detail/'.\$article['id'];
?>
php;
		return $php;
		});
		Blade::directive ('endarticle',function(){
			$php=<<<php
<?php 
}
?>
php;
			return $php;
		});
	}

	/**
	 * 自定义标签，用来获取分类
	 */
	public function category(){
		Blade::directive ('category',function($canshu){
			$canshu=$canshu?:'[]';
			$php=<<<php
<?php
\$params=$canshu;
\$db=new \Modules\Shop\Entities\Category();
if(isset(\$params['pid'])){
\$db=\$db->where('pid',\$params['pid']);
}
\$cate=\$db->get();
foreach(\$cate as \$v){
\$v['url']='/list/'.\$v['id'];
?>
php;
			return $php;
		});

		Blade::directive ('endcategory',function(){
			$php=<<<php
<?php
}
?>
php;
			return $php;
		});
	}

	/**
	 * 自定义标签，用来获取所有文章标签
	 */
	public function tag(){
		Blade::directive ('tag',function($aid){
			$aid=$aid?:'[]';
			$php=<<<php
<?php 
	\$params=$aid;
	\$db=new Modules\Shop\Entities\Tag();
	if(isset(\$params['aid'])){
		 \$tag_id=\Modules\Shop\Entities\TagArticle::where('article_id',\$params['aid'])->pluck('tag_id');
		\$db=\$db->whereIn('id',\$tag_id);
	}
	\$tags=\$db->get();
	foreach(\$tags as \$tag){
	\$tag['url']='/tag/'.\$tag['id'];
?>
php;
		return $php;
		});
		Blade::directive ('endtag',function(){
			$php=<<<php
<?php 
}
?>
php;
		return $php;
		});
	}
	//定义标签获得当前所显示文章的所属分类
	public function currentCategory(){
		Blade::directive ('curCategory',function($pid){
			$php=<<<php
<?php
			\$params=$pid;
		\$db=new \Modules\Shop\Entities\Category();
		\$cur=\$db::where('id',\$params['pid'])->first();
		echo "<a href='/list/".\$params['pid']."'>".\$cur['cname']."</a>";
?>
php;
			return $php;
	});
	}
	//定义标签获得上一篇和下一篇
	public function pre_next(){
		Blade::directive ('pre_next',function($canshu){
			$php=<<<php
<?php
			\$params=$canshu;
		\$db=new \Modules\Shop\Entities\Article();
		if(isset(\$params['pre'])){
			\$db=\$db->where('id','<',\$params['pre'])->limit(1);
		}
		if(isset(\$params['next'])){
			\$db=\$db->where('id','>',\$params['next'])->limit(1);
		}
		\$p_n=\$db->first();
		if(\$p_n){
			echo "<a href='/detail/".\$p_n['id']."'>".\$p_n['title']."</a>";
		}else{
			echo "没有了";
		}
		
?>
php;
			return $php;
		});
	}

	public function friend(){
		Blade::directive ('friend',function(){
			$php=<<<php
<?php 
	\$db=new Modules\Shop\Entities\Friend();
		\$db=\$db->where('isshow',1);
	\$friends=\$db->get();
	foreach(\$friends as \$friend){
?>
php;
			return $php;
		});
		Blade::directive ('endfriend',function(){
			$php=<<<php
<?php 
}
?>
php;
			return $php;
		});
	}
}